package cn.com.kgo.usermanamge.service.impl;

import cn.com.kgo.common.api.usermanage.UserService;
import cn.com.kgo.entity.usermanage.UserEntity;
import cn.com.kgo.entity.util.KResult;
import cn.com.kgo.usermanamge.dao.UserDao;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Component;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/5/1715:13
 */
@Component
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public String getUser(UserEntity userEntity) {
        if (ObjectUtil.isNull(userEntity)|| StringUtils.isBlank(userEntity.getId())){
            return KResult.queryFail("查询失败，查询凭证【Id】不能为空！");
        }
        UserEntity user =  userDao.selectById(userEntity.getId());
        if (ObjectUtil.isNotNull(user)){
            return KResult.querySucceed(1,user,"查询成功");
        }else {
            return KResult.queryFail("查询失败，无该用户信息！");
        }

    }
    @Override
    public String addUser(UserEntity userEntity) {
        userEntity.setId(IdUtil.fastSimpleUUID());
        if(StringUtils.isBlank(userEntity.getUserName())) return KResult.failAdd("新增失败！【用户名不能为空】");
        if(StringUtils.isBlank(userEntity.getPassword())) return KResult.failAdd("新增失败！【登录密码不能为空】");
        int isExistUser = userDao.getUserCountByUserName(userEntity.getUserName());
        if(isExistUser>0)return KResult.failAdd("新增失败！【该用户名已经存在，请重新输入】");
        int count = userDao.insert(userEntity);
        return KResult.add(count);
    }

}
